
import java.util.Scanner;

public class Demo01 {
	//采用二维数组存放用户信息（部门、用户名、密码、用户角色）
			static String [][] users = new String[20][4];
			//书籍信息（编码（ISBN）、书籍名称、价格、出版社、作者）
			static String [][] books = new String[20][5];
			//出版社信息（出版社名称、地址、联系人）
			static String [][] publics = new String[20][3];

			static Scanner sc = new Scanner(System.in);
			
			public static void main(String[] args) {
				//主方法
				inti();
								
				welcome();
			}
			private static void welcome() {
				// TODO Auto-generated method stub
				System.out.println("欢迎来到闽大图书管理系统！");
				System.out.println("1.登录 2.注册");
				
				int key = sc.nextInt();
				switch (key) {
				case 1:
					//登录
					login();
					
					break;
				case 2:
					//注册
					register();
					
					break;

				default:
					break;
				}
			}


			private static void register() {
				// 注册
				System.out.println("请输入所属部门");
				String depName = sc.next();
				System.out.println("请输入用户名");
				String userName = sc.next();
				System.out.println("请输入密码");
				String passWord = sc.next();
				
				int index = FirstNull(users);
				
				users[index][0] = depName;
				users[index][1] = userName;
				users[index][2] = passWord;
				users[index][3] = "程序员";
				
				System.out.println("注册成功!");
				welcome();
			}


			private static int FirstNull(String[][] arr) {
				// 第一个空值
				int index = -1;
				
				for (int i = 0; i < arr.length; i++) {
					if (arr[i][0]==null) {
						index = i;
					}
				}
				return index;
			}


			private static void login() {
				// 登录
				System.out.println("请输入用户名：");
				String userName = sc.next();
				System.out.println("请输入密码：");
				String passWord = sc.next();
				
				boolean flag = LoginInfo(userName,passWord);
				
				if (flag==false) {
					System.out.println("密码记错了SB，1.重新输入 2.注册");
					
					judge();
				}else {
					System.out.println(userName+"，"+"欢迎使用闽大图书管理系统！！！");
					homePage();
				}
			}


			private static void homePage() {
				// 主页
				System.out.println("请输入数字进行选择：1 图书管理  2 出版社管理  3 退出登录  4 退出系统");
				
				int key = sc.nextInt();
				select(key);
			}


			private static void select(int key) {
				// 主页功能选择
				//1 图书管理  2 出版社管理  3 退出登录  4 退出系统
				switch (key) {
				case 1:
					//图书管理
					BookManage();
					break;
				case 2:
					//出版社管理
					PublicManage();
					break;
				case 3:
					//退出登录
					outDL();
					break;
				case 4:
					//退出系统
					outXT();
					break;

				default:
					break;
				}
				
			}


			private static void outXT() {
				// 退出系统
				System.exit(0);
				
			}
			private static void outDL() {
				// 退出登录
				System.out.println("已退出登录！");
				welcome();
				
			}
			private static void PublicManage() {
				// 出版社管理
				System.out.println("请输入：1.增加 2.删除 3.更新 4.根据出版社名称查询"
						+ " 5.查询所有出版社 6.返回上一级菜单");
				int key = sc.nextInt();
				
				switch (key) {
				case 1:
					//添加
					add2(publics);
					break;
				case 2:
					//删除
					delete2(publics);
					break;
				case 3:
					//更新
					update2(publics);
					break;
				case 4:
					//根据出版社名称查询
					inquireName(publics);
					break;
				case 5:
					//查询所有出版社
					inquirePublic(publics);
					break;
				case 6:
					//返回上一级菜单
					homePage();
					break;
				
				default:
					break;
				}
			}
			private static void inquirePublic(String[][] publics2) {
				// 查询所有出版社
				System.out.println("出版社名称"+"\t"+"地址"+"\t"+"联系人");
				for (int i = 0; i < publics2.length; i++) {
					if (publics[i][0]!=null) {
						System.out.println(publics[i][0]+"\t"+
							publics[i][1]+"\t"+publics[i][2]);
					}
				}
				homePage();
			}
			private static void inquireName(String[][] publics) {
				// 出版社名称查询
				System.out.println("请输入出版社名称：");
				String publicName = sc.next();
				int index = FirstNull(publics);
				publics[index][0] = publicName;
				
				System.err.println(publics[index][0]);
				
				for (int i = 0; i < publics.length; i++) {
					if (index != -1) {
						if (publicName.equals(publics[i][0])) {
							System.out.println("出版社名称："+publics[index][0]);
							System.out.println("出版社地址："+publics[index][1]);
							System.out.println("出版社联系人："+publics[index][2]);
							
							break;
						}
						}else {
						System.out.println("该出版社不存在！！！");
						break;
					}
					}
					homePage();
				}
				
//				for (int i = 0; i < publics.length; i++) {
//					if (publics[i][0]!=null) {
//						if (publicName.equals(publics[i][0])) {
//							System.out.println("出版社名称："+publics[i][0]);
//							System.out.println("出版社地址："+publics[i][1]);
//							System.out.println("出版社联系人："+publics[i][2]);
//						}else {
//							System.out.println("该出版社不存在！！！");
//						}
//					}
//					
//				}
				
//			}
			private static void update2(String[][] publics) {
				// 更新书籍--出版社
			
				System.out.println("请输入要更新的出版社名称：");
				String publicName = sc.next();
				
				int index = FirstNull(publics);
				
				
				if (index != -1) {
					System.out.println("出版社名称"+"\t"+"地址"+"\t"+"联系人");
					System.out.println(publics[index][0]+"\t"+publics[index][1]+"\t"+publics[index][2]);
					
					System.out.println("请输入要更新的地址：");
					publics[index][1] = sc.next();
					System.out.println("请输入要更新的联系人姓名：");
					publics[index][2] = sc.next();
				
					System.out.println("更新成功");
					System.out.println("出版社名称"+"\t"+"地址"+"\t"+"联系人");
					System.out.println(publics[index][0]+"\t"+publics[index][1]+"\t"+publics[index][2]);
				
				}else {
					System.out.println("出版社不存在！");
				}
				homePage();
				
			}
			private static void delete2(String[][] publics2) {
				// 删除书籍--出版社
				System.out.println("请输入要删除的出版社名称：");
				String publicName = sc.next();
				
				boolean flag = false;
				for (int i = 0; i < books.length; i++) {
					if (publicName.equals(books[i][3])) {
						flag = true;
						break;
					}
					if (flag) {
						System.out.println("该出版社有相关图书信息存在！"
						+ "不可以删除！如果要删除请先删除所有相关图书信息！！！");
					}else {
						int index = FirstNull(publics);
						publics[index][0] = null;
						publics[index][1] = null;
						publics[index][2] = null;
						
						System.out.println("删除成功!");
						break;
					}
				}
				homePage();
			}
			private static void add2(String[][] publics2) {
				// 添加书籍--出版社
				System.out.println("请输入出版社名称：");
				String publicName = sc.next();
				System.out.println("请输入出版社地址：");
				String publicAdress = sc.next();
				System.out.println("请输入出版社联系人：");
				String publicPeople = sc.next();
				
				int index = FirstNull(publics);
				
				publics[index][0] = publicName;
				publics[index][1] = publicAdress;
				publics[index][2] = publicPeople;
				
				System.out.println("出版社添加成功");
				
				homePage();
			}
			private static void BookManage() {
				// 图书管理
				System.out.println("请输入：1.增加 2.删除 3.更新 4.查询 5.返回上一级菜单");
				int key = sc.nextInt();
				
				switch (key) {
				case 1:
					//添加书籍
					add(books);
					break;
				case 2:
					//删除书籍
					delete(books);
					break;
				case 3:
					//更新书籍
					update(books);
					break;
				case 4:
					//查询书籍
					inquire(books);
					break;
				case 5:
					//返回上一级
					homePage();
					break;

				default:
					break;
				}
			}

			private static void inquire(String[][] books2) {
				// 查询
				System.out.println("请输入查询种类：1.isbn 2.书名（模糊） 3.出版社 4. 作者 "
									+ "5. 价格范围 6.查询所有 7.返回上一级");
				int key = sc.nextInt();
				switch (key) {
				case 1:
					//ISBN
					ISBN();
					break;
				case 2:
					//书名（模糊）
					bookName();
					break;
				case 3:
					//出版社
					publics();
					break;
				case 4:
					//作者
					author();
					break;
				case 5:
					//价格范围
					price();
					break;
				case 6:
					//查询所有
					all();
					break;
				case 7:
					//返回上一级
					BookManage();
					break;

				default:
					break;
				}
				
			}
			private static void bookName() {
				// 查询--书名（模糊）
				System.out.println("请输入书名：");
				String bookName = sc.next();
				System.out.println("ISBN"+"\t"+"书名"+"\t"+"价格"+"\t"+
									"出版社"+"\t"+"作者");
				for (int i = 0; i < books.length; i++) {
					if (books[i][1]!=null) {
						if (books[i][1].indexOf(bookName)!=-1) {
							System.out.println(books[i][0]+"\t"+books[i][1]+"\t"+
									books[i][2]+"\t"+books[i][3]+"\t"+books[i][4]);
						}
					}
				}
				inquire(books);
			}
			private static void all() {
				// 查询--所有
				for (int i = 0; i < books.length; i++) {
					if (books[i][0]!=null) {
						System.out.println("ISBN"+"\t"+"书名"+"\t"+"价格"+"\t"+
											"出版社"+"\t"+"作者");
						System.out.println(books[i][0]+"\t"+books[i][1]+"\t"+
							books[i][2]+"\t"+books[i][3]+"\t"+books[i][4]);
					}
				}
				inquire(books);
			}
			private static void price() {
				// 查询--价格
				System.out.println("请输入最低价格");
				double minPrice = sc.nextDouble();
				System.out.println("请输入最高价格");
				double maxPrice = sc.nextDouble();
				for (int i = 0; i < books.length; i++) {
					if (books[i][2]!=null) {
						double price = Double.parseDouble(books[i][2]);
						if (price >= minPrice && price <= maxPrice) { 
							System.out.println("ISBN"+"\t"+"书名"+"\t"+"价格"+"\t"+"出版社"+"\t"+"作者");
							System.out.println(books[i][0]+"\t"+books[i][1]+"\t"+books[i][2]+"\t"+books[i][3]+"\t"+books[i][4]);
						}
					}
				}
				inquire(books);
			}
			private static void author() {
				// 查询--作者
				System.out.println("请输入作者姓名：");
				String author = sc.next();
				for (int i = 0; i < books.length; i++) {
					if (author.equals(books[i][4])) {
						System.out.println("ISBN"+"\t"+"书名"+"\t"+"价格"+"\t"+"出版社"+"\t"+"作者");
						System.out.println(books[i][0]+"\t"+books[i][1]+"\t"+books[i][2]+"\t"+books[i][3]+"\t"+books[i][4]);
						
					}
				}
				inquire(books);
				
			}
			private static void publics() {
				// 出版社查询   inquirePublics
				System.out.println("请输入你要查询的出版社名称");
				String publicName = sc.next();
				
				for (int i = 0; i < books.length; i++) {
					if (publicName.equals(books[i][3])) {
						System.out.println("ISBN"+"\t"+"书名"+
							"\t"+"价格"+"\t"+"出版社"+"\t"+"作者");
						System.out.println(books[i][0]+"\t"+
											books[i][1]+"\t"+
											books[i][2]+"\t"+
											books[i][3]+"\t"+
											books[i][4]);
					}
				}
				inquire(books);
				
			}
					
			private static void ISBN() {
				// 查询 inquire ISBN
				System.out.println("请输入ISBN号：");
				String isbn = sc.next();
				int index = FirstNull(books);
				
				for (int i = 0; i < books.length; i++) {
					if (books[i][0]!=null) {
						if (isbn.equals(books[i][0])) {
							System.out.println("ISBN"+"\t"+"书名"+"\t"+"价格"+"\t"+"出版社"+"\t"+"作者");
							System.out.println(books[i][0]+"\t"+books[i][1]+"\t"+books[i][2]+"\t"+books[i][3]+"\t"+books[i][4]);
							books[index][0] = isbn;
						}
					}
					
				}
				inquire(books);
				
			}
			private static void update(String[][] books2) {
				// 更新书籍
				System.out.println("请输入ISBN号：");
				String ISBN = sc.next();
				
				int index = -1;
				boolean flag = false;
				for (int i = 0; i < books.length; i++) {
					if (ISBN.equals(books[i][0])) {
						index = i;
						flag = true;
						break;
					}
				}
				if (flag) {
					System.out.println("请输入新的书名：");
					String bookName = sc.next();
					System.out.println("请输入新的价格：");
					String bookPrice = sc.next();
					System.out.println("请输入新的出版社：");
					String bookPublic= sc.next();
					System.out.println("请输入新的作者：");
					String bookAuthor = sc.next();
					
					books[index][1] = bookName;
					books[index][2] = bookPrice;
					books[index][3] = bookPublic;
					books[index][4] = bookAuthor;
				}else {
					System.out.println("该ISBN号不存在！！！");
				}
				BookManage();
			}

			private static void delete(String[][] books2) {
				//删除书籍
				System.out.println("请输入要删除的书本名称：");
				String deleteBook = sc.next();
				boolean flag = false;
				
				for (int i = 0; i < books.length; i++) {
					if (deleteBook.equals(books[i][1])) {
						books[i][0] = null;
						books[i][1] = null;
						books[i][2] = null;
						books[i][3] = null;
						books[i][4] = null;
						System.out.println("删除成功");
						flag = true;
						break;
					}
				}
				if (flag==false) {
					System.out.println("没有找到该书！");
					System.out.println("删除失败");
				}
				BookManage();
			}

			private static void add(String[][] books2) {
				// 添加书籍
				System.out.println("请输入图书ISBN:");
				String ISBN = sc.next();
				System.out.println("请输入书名:");
				String bookName = sc.next();
				System.out.println("请输入价格:");
				String bookPrice = sc.next();
				System.out.println("请输入出版社:");
				String bookPublic = sc.next();
				System.out.println("请输入作者:");
				String bookAuthor = sc.next();
				
				int index = FirstNull(books);
				
				books [index][0] = ISBN;
				books [index][1] = bookName;
				books [index][2] = bookPrice;
				books [index][3] = bookPublic;
				books [index][4] = bookAuthor;
				System.out.println("添加成功！！！");
				
				BookManage();
			}

			private static void judge() {
				// 欢迎判断
				int key = sc.nextInt();
				
				switch (key) {
				case 1:
					login();
					break;
				case 2:
					register();
					break;

				default:
					System.out.println("你输入错了，1.重新输入 2.注册");
					judge();
					break;
				}
			}


			private static boolean LoginInfo(String userName, String passWord) {
				// 用户登录信息判断
				boolean flag = false;
				for (int i = 0; i < users.length; i++) {
					if (userName.equals(users[i][1])) {
						if (passWord.equals(users[i][2])) {
							flag = true;
						}
					}
				}
				return flag;
			}


			private static void inti() {
				// 初始化
				
				//用户信息（部门、用户名、密码、用户角色）
				users[0][0] = "开发部";
				users[0][1] = "admin";
				users[0][2] = "123";
				users[0][3] = "程序员";
				
				users[1][0] = "销售部";
				users[1][1] = "hahaha";
				users[1][2] = "123";
				users[1][3] = "推销员";
				
				users[2][0] = "娱乐部";
				users[2][1] = "a";
				users[2][2] = "1";
				users[2][3] = "混子";
				
				//籍信息（编码（ISBN）、书籍名称、价格、出版社、作者）
				books[0][0] = "123";
				books[0][1] = "hhh";
				books[0][2] = "12";
				books[0][3] = "闽大";
				books[0][4] = "你猜";
				
				books[1][0] = "456";
				books[1][1] = "如何让富婆爱上你";
				books[1][2] = "22";
				books[1][3] = "脱单协会";
				books[1][4] = "单身贵族";
				
				//出版社信息（出版社名称、地址、联系人）
				publics[0][0] = "闽大";
				publics[0][1] = "闽大路8号";
				publics[0][2] = "aaa";
				
				publics[1][0] = "脱单协会";
				publics[1][1] = "梦里";
				publics[1][2] = "单身狗";
			}

}